package com.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.youxuan.mapper.CartDetailMapper;
import com.youxuan.mapper.CartMapper;
import com.youxuan.pojo.entity.Cart;
import com.youxuan.pojo.entity.CartDetail;
import com.youxuan.pojo.entity.Goods;
import com.youxuan.pojo.vo.AddCartGoodsParamVo;
import com.youxuan.pojo.vo.GoodsVo;
import com.youxuan.service.CartDetailService;
import com.youxuan.service.CartService;
import com.youxuan.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private CartDetailService cartDetailService;
    @Autowired
    private GoodsService goodsService;

    @Override
    public List<GoodsVo> getUserCartGoodsList(Long uid) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUid, uid);
        Cart cart = cartMapper.selectOne(queryWrapper);

        if (cart == null) {
            cart = new Cart();
            cart.setUid(uid);
            cartMapper.insert(cart);
        }
        Long cartId = cart.getId();
        List<CartDetail> cartDetails = cartDetailService.getCartDetailByCid(cartId);

        List<GoodsVo> GoodsVo = new ArrayList<>();
        if (cartDetails != null && cartDetails.size() > 0) {
            GoodsVo = cartDetails.stream().map(cartDetail -> {
                GoodsVo goodsVo = new GoodsVo();
                Long goodsId = cartDetail.getGoodsId();
                Goods goods = goodsService.getGoods(goodsId);
                BeanUtils.copyProperties(goods, goodsVo);
                goodsVo.setNum(cartDetail.getNum());
                goodsVo.setIsSelect(cartDetail.getIsSelect());
                return goodsVo;
            }).collect(Collectors.toList());
        }


        return GoodsVo;
    }

    @Override
    public void addCartGoods(AddCartGoodsParamVo addCartGoodsParamVo) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUid, addCartGoodsParamVo.getUserId());
        Cart cart = cartMapper.selectOne(queryWrapper);

        if (cart == null) {
            cart = new Cart();
            cart.setUid(addCartGoodsParamVo.getUserId());
            cartMapper.insert(cart);
        }

        CartDetail cartDetail = cartDetailService.getCartDetailByCidAndGid(cart.getId(),addCartGoodsParamVo.getGoodsId());
        if (cartDetail == null) {
            cartDetail = new CartDetail();
            BeanUtils.copyProperties(addCartGoodsParamVo,cartDetail);
            cartDetail.setCartId(cart.getId());
            cartDetail.setIsSelect("1");
            cartDetailService.addCartGoods(cartDetail);
        }else{
            cartDetail.setNum(cartDetail.getNum() + addCartGoodsParamVo.getNum());
            cartDetailService.changeCartGoodsNum(cartDetail);
        }

    }
}
